// splitter.txt
// Like basicnpc, but when it dies, it splits up into up to 4 other creatures.

// Memory Cells:
//   Cell 0 - If 0, default behavior. If 1, it doesnt fidget.
//   Cell 1,2 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.
//   Cell 3,4,5,6 - Creatures to spawn when it dies. leave at 0 for none.

begincreaturescript;

variables;

short i,target;
short health_level;
short worm_mes = 0;
short att1 = 0;
short att2 = 0;
short att3 = 0;
short r1;

body;

beginstate INIT_STATE;
	set_name(ME,"Wormhost Rotghroth");
	set_level(ME,40);
	change_max_health(ME,200);
	set_boss_level(ME,2);
	set_attack_bonus(ME,10);
	set_new_abil(ME,15);
	
	health_level = get_health(ME);
	break;

beginstate DEAD_STATE;
	force_char_status(1000,6,30);

	
	summon_creature(37);
	summon_creature(38);
	summon_creature(39);
	summon_creature(40);
	set_level(45,30);
	set_level(46,30);
	set_level(47,30);
	set_level(48,30);
	set_attack_bonus(45,40);
	set_new_abil(45,20);
	set_attack_bonus(46,40);
	set_new_abil(46,20);
	set_attack_bonus(47,40);
	set_new_abil(47,20);
	set_attack_bonus(48,40);
	set_new_abil(48,20);
	set_resistance(45,7,75);
	set_resistance(46,7,75);
	set_resistance(47,7,75);
	set_resistance(48,7,75);
	force_char_status(45,1,20);
	force_char_status(46,1,20);
	force_char_status(47,1,20);
	force_char_status(48,1,20);
	
	begin_talk_mode(6);
break;

beginstate START_STATE; 
	// if I have a target for some reason, go attack it
	if (target_ok()) {
		if (dist_to_char(get_target()) <= 16)
			set_state(3);
			else set_foe_target(ME,-1);
		}
	
	if (get_foe_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	if ((get_attitude(ME) >= 10) && (gf(81,4) > 0)) {
		set_foe_target(ME,pc_num());
		set_state(3);
		}


	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (target_ok() == FALSE)
		set_state(START_STATE);

	if ((att1 == 0) && (get_health(ME) < (get_max_health(ME) * 3) / 4)) {
		att1 = 1;
		set_attitude(29,10);
		set_attitude(30,10);
		set_attitude(31,10);
		set_attitude(32,10);
		set_act_at_dist(29,1);
		set_act_at_dist(30,1);
		set_act_at_dist(31,1);
		set_act_at_dist(32,1);
		}

	if ((att2 == 0) && (get_health(ME) < (get_max_health(ME) * 2) / 4)) {
		att2 = 1;
		set_attitude(33,10);
		set_attitude(34,10);
		set_attitude(35,10);
		set_act_at_dist(33,1);
		set_act_at_dist(34,1);
		set_act_at_dist(35,1);
		}

	if ((att3 == 0) && (get_health(ME) < (get_max_health(ME) * 1) / 4)) {
		att3 = 1;
		set_attitude(36,10);
		set_attitude(37,10);
		set_attitude(38,10);
		set_attitude(39,10);
		set_act_at_dist(36,1);
		set_act_at_dist(37,1);
		set_act_at_dist(38,1);
		set_act_at_dist(39,1);
		}
		
	if (get_health(ME) < health_level - 50) {
		health_level = get_health(ME);
		r1 = get_ran(1,0,3);
		if (char_ok(41 + r1))
			r1 = get_ran(1,0,3); 
		if (char_ok(41 + r1) == FALSE) {
			if (summon_creature(33 + r1)) {
				print_str_color("A worm burrows out of the rotghroth. Ugh!",3);
				
				if (get_ran(1,0,1) == 0) {
					set_name(41 + r1,"Plated Burrower");
					set_resistance(41 + r1,0,90);
					}
					else {
						set_name(41 + r1,"Moist Burrower");
						set_resistance(41 + r1,1,90);
						set_resistance(41 + r1,2,90);
						set_resistance(41 + r1,6,90);
						}
				set_summon_level(41 + r1,1);
				set_level(41 + r1,25);
				set_boss_level(41 + r1,-1);
				place_particle_num(41 + r1,10,7,10);
				set_attack_bonus(41 + r1,40);
				set_new_abil(41 + r1,20);
				set_resistance(41 + r1,7,75);
				force_char_status(41 + r1,1,20);
				
				run_char_animation(2,1,50);	
				pc_heard_sound_delay(136,100);						
				
				if (worm_mes == 0) {
					worm_mes = 1;
					begin_talk_mode(5);
					}
				end();
				}
			}
		}

	do_attack();
break;

beginstate TALKING_STATE;
	print_str("Talking: It doesn't respond.");
break;